﻿using Dapper.Extensions;
using System;
using Ymatou.ServiceCenter.Domain.Models;

namespace Ymatou.ServiceCenter.Repository
{
    public sealed class ServiceDependencyMapper : ClassMapper<ServiceDependency>
    {
        public ServiceDependencyMapper()
        {
            TableName = "servicedependency";
            AutoMap();
        }
    }

    public sealed class ServiceConfigMapper : ClassMapper<ServiceConfig>
    {
        public ServiceConfigMapper()
        {
            TableName = "serviceconfig";
            MapProperty(p => p.ConsumerAppId).Key(KeyType.Assigned);
            MapProperty(p => p.ProviderAppId).Key(KeyType.Assigned);
            BeforeSaveAction = (entity) =>
            {
                if (entity == null)
                    return;
                //做一下日期兼容处理
                if (entity.CreateTime == DateTime.MinValue)
                    entity.CreateTime = new DateTime(1900, 1, 1);
                
                entity.LastUpdateTime = DateTime.Now;
                entity.LastUpdateTicks = DateTime.Now.Ticks;
            };
            AutoMap();
        }
    }

    public sealed class ServiceMetadataMapper : ClassMapper<ServiceMetadata>
    {
        public ServiceMetadataMapper()
        {
            TableName = "servicemetadata";
            MapProperty(p => p.AppId).Key(KeyType.Assigned);
            MapProperty(p => p.ApiName).Key(KeyType.Assigned);
            MapProperty(p => p.HttpMethod).Key(KeyType.Assigned);
            BeforeSaveAction = (entity) =>
            {
                if (entity == null)
                    return;
                //做一下日期兼容处理
                if (entity.CreateTime == DateTime.MinValue)
                    entity.CreateTime = new DateTime(1900, 1, 1);
                
                entity.LastUpdateTime = DateTime.Now;
                entity.LastUpdateTicks = DateTime.Now.Ticks;
            };
            AutoMap();
        }
    }

    public sealed class ServiceDependencySamplingMapper : ClassMapper<ServiceDependencySampling>
    {
        public ServiceDependencySamplingMapper()
        {
            TableName = "servicedependencysampling";
            MapProperty(p => p.Id).Key(KeyType.Identity);
            BeforeSaveAction = (entity) =>
            {
                if (entity == null)
                    return;
                //做一下日期兼容处理
                if (entity.BeginTime == DateTime.MinValue)
                    entity.BeginTime = new DateTime(1900, 1, 1);
                
                entity.EndTime = DateTime.Now;
               
            };
            AutoMap();
        }
    }

    public sealed class ServicedependencyInfoMapper : ClassMapper<ServicedependencyInfo>
    {
        public ServicedependencyInfoMapper()
        {
            TableName = "servicedependency_info";
            MapProperty(p => p.ConsumerAppId).Key(KeyType.Assigned);
            MapProperty(p => p.ProviderAppId).Key(KeyType.Assigned);
            BeforeSaveAction = (entity) =>
            {
                if (entity == null)
                    return;
                //做一下日期兼容处理
                if (entity.CreateTime == DateTime.MinValue)
                    entity.CreateTime = new DateTime(1900, 1, 1);

                entity.LastUpdateTime = DateTime.Now;
            };
            AutoMap();
        }
    }

    public sealed class RpcClientConfigMapper : ClassMapper<RpcClientConfig>
    {
        public RpcClientConfigMapper()
        {
            TableName = "rpcclientconfig";
            MapProperty(p => p.AppId).Key(KeyType.Assigned);
            BeforeSaveAction = (entity) =>
            {
                if (entity == null)
                    return;
                //做一下日期兼容处理
                if (entity.CreateTime == DateTime.MinValue)
                    entity.CreateTime = new DateTime(1900, 1, 1);

                entity.LastUpdateTime = DateTime.Now;
            };
            AutoMap();
        }
    }

    //示例
    //public sealed class RoleEntityMapper : ClassMapper<RoleEntity>
    //{
    //    public RoleEntityMapper()
    //    {
    //        TableName = "Role";
    //        BeforeSaveAction = (entity) =>
    //        {
    //            Console.WriteLine(string.Format("Role,BeforeSave,Name={0}", entity.Name));
    //        };
    //        AfterSaveAction = (entity) =>
    //        {
    //            Console.WriteLine(string.Format("Role,AfterSave,Name={0}", entity.Name));
    //        };
    //        MapProperty(p => p.Id).Key(KeyType.Identity);
    //        MapProperty(p => p.Version).Version();
    //        //MapProperty(p => p.IsPersisted).Persisted();
    //        AutoMap();
    //    }
    //}
}
